using System.Globalization;
using System.Text.RegularExpressions;

namespace Thunder.Irc.Formatter.Default
{
    /// <summary>
    /// Formats hyperlinks in messages
    /// </summary>
    internal class GenericFormatterHyperlink : GenericFormatter
    {
        /// <summary>
        /// This method is invoked when a message should be formatted
        /// </summary>
        /// <param name="args">The <see cref="CancelMessageFormattingEventArgs"/> containing the message to be passed</param>
        /// <remarks>You can cancel further processing of the message by setting the <see cref="CancelMessageFormattingEventArgs.Cancel"/> to true</remarks>
        protected override void FormatCore(MessageFormattingEventArgs args)
        {
            Regex regex = new Regex(@"(ht|f)tp(s?):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&amp;:/~\+#]*[\w\-\@?^=%&amp;/~\+#])?");
            args.Message = regex.Replace(args.Message, match => string.Format(CultureInfo.InvariantCulture, @"<ThunderIrc:ThunderHyperlink xmlns:ThunderIrc=""clr-namespace:Thunder.IrcClient;assembly=Thunder.IrcClient"" NavigateUri=""{0}"">{0}</ThunderIrc:ThunderHyperlink>", match.Captures[0].Value));
        }
    }
}